package com.smart.village.big.screen.controller.bigscreen;

import com.smart.village.big.screen.domain.AjaxResult;
import com.smart.village.big.screen.service.NewsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Logger;

import static com.smart.village.big.screen.domain.AjaxResult.error;
import static com.smart.village.big.screen.domain.AjaxResult.success;


/**
 * 新闻类
 */
@Tag(name = "新闻类数据接口")
@RestController
@RequestMapping("/news")
public class NewsController {

    Logger logger = Logger.getLogger(NewsController.class.getName());

    @Autowired
    private NewsService newsService;

    /**
     * 根据新闻主键 获取详情
     *
     * @param newsSerialNumber
     * @return
     */
    @Operation(summary = "根据新闻序列号 获取详情")
    @GetMapping("/selectByPrimaryKey/{newsSerialNumber}")
    @ResponseBody
    public AjaxResult selectByPrimaryKey(@PathVariable("newsSerialNumber") @Parameter(description = "新闻序列号") String newsSerialNumber) {
        try {
            return success(newsService.selectByPrimaryKey(newsSerialNumber));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 获取大屏 时政要闻 新闻列表
     * 根据不同的分类 每个分类获取 最新的10条数据
     *
     * @return
     */
    @Operation(summary = "获取大屏 时政要闻主模块 新闻列表 ,更多接口，带分页")
    @GetMapping("/getList")
    @ResponseBody
    public AjaxResult getList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "主分类序列号") String newsCategorySerialNumber,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false) @Parameter(description = "权限字段") String partyOrganizationActivitySerialNumber,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            //environment 0 大屏 1 小程序
            return success(newsService.getList(newsCategorySerialNumber, partyOrganizationActivitySerialNumber, pageNum, pageSize, search, 0,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }


    /**
     * 获取大屏 工作动态 新闻列表
     * 根据不同的标签 每个标签获取 最新的10条数据
     *
     * @param newsTagSerialNumber
     * @return
     */
    @Operation(summary = "获取大屏 工作动态主模块 新闻列表, 根据不同的标签 每个标签获取 最新的10条数据")
    @GetMapping("/getWorkDynamicList")
    @ResponseBody

    public AjaxResult getWorkDynamicList(
            @RequestParam @Parameter(description = "终端") String NewsTerminal,
            @RequestParam(required = false) @Parameter(description = "标签序列号") String newsTagSerialNumber,
            @RequestParam(required = false) @Parameter(description = "搜索") String search,
            @RequestParam(required = false, defaultValue = "1") @Parameter(description = "当前页数") int pageNum,
            @RequestParam(required = false, defaultValue = "10") @Parameter(description = "每页条数") int pageSize
    ) {
        try {
            if (ObjectUtils.isEmpty(pageNum) || pageNum == 0) {
                pageNum = 1;
            }
            if (ObjectUtils.isEmpty(pageSize) || pageSize == 0) {
                pageSize = 10;
            }
            //environment 0 大屏 1 小程序
            return success(newsService.getWorkDynamicList(newsTagSerialNumber, pageNum, pageSize, search, 0,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 时政要闻 工作动态轮播图
     *
     * @return
     */
    @Operation(summary = "获取大屏 时政要闻 工作动态轮播图")
    @GetMapping("/getWorkDynamicBannerList")
    @ResponseBody

    public AjaxResult getWorkDynamicBannerList(
            @RequestParam(required = false, defaultValue = "2") @Parameter(description = "要获取banner的条数") int cnt
            ,@RequestParam @Parameter(description = "终端") String NewsTerminal
    ) {
        try {

            if (ObjectUtils.isEmpty(cnt) || cnt == 0) {
                cnt = 10;
            }
            return success(newsService.getWorkDynamicBannerList(cnt, 0,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 工作动态 党建工作成果
     *
     * @return
     */
    @Operation(summary = "获取大屏 工作动态 党建工作成果")
    @GetMapping("/getWorkResults")
    @ResponseBody

    public AjaxResult getWorkResults(
            @RequestParam @Parameter(description = "终端") String NewsTerminal
    ) {
        try {
            return success(newsService.getWorkResults(0,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    /**
     * 获取大屏 工作动态 信息发布统计
     *
     * @return
     */
    @Operation(summary = "获取大屏 工作动态 信息发布统计")
    @GetMapping("/getInformationPublishStatics")
    @ResponseBody
    public AjaxResult getInformationPublishStatics(
            @RequestParam @Parameter(description = "终端") String NewsTerminal
    ) {
        try {
            return success(newsService.getInformationPublishStatics("NC0000000000036", 0,NewsTerminal));
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(e.getMessage());
            return error("程序错误请联系系统管理员");
        }
    }

    @Operation(summary = "成果标签列表")
    @Parameters({
    })
    @GetMapping("getNewsTagsChengGuoList")
    @ResponseBody
    public AjaxResult getNewsTagsChengGuoList(

    ) {
        try {

            return success(newsService
                    .getNewsTagsChengGuoList()
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe("首页成果标签列表失败: " + e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }

    @Operation(summary = "首页轮播-大屏")
    @Parameters({
    })
    @GetMapping("getCarouselConfig")
    @ResponseBody
    public AjaxResult getCarouselConfig(
    ) {
        try {

            return success(newsService
                    .getCarouselConfigBigScreen()
            );
        } catch (Exception e) {
            e.printStackTrace();
            logger.severe("首页轮播-大屏失败: " + e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
